﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using EasyClassUI.entity;
using Newtonsoft.Json;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace EasyClassUI.Utils {
    /// <summary>
    /// TypeToSting Function：将各种数据格式序列化成字符串存储以及反序列化
    /// <---------------------------------------------------------------------------------->
    /// User与StudyGroup是多对多的关系，但是由于创建数据库的时候只建立一对多的关系
    /// 采用的策略是利用序列化的知识将每一个StudyGroup的成员列表序列化成一个字符串存储在数据库中
    /// 在需要取用的时候进行反序列化的操作
    /// <---------------------------------------------------------------------------------->
    /// </summary>
    public static class FileManagement
    {

        /// <summary>
        /// 写入到文件
        /// </summary>
        /// <param name="FilePath">文件绝对路径</param>
        /// <param name="FileContent">文件内容</param>
        public static void WriteToFile(String FilePath, String FileContent)
        {
            if (!Directory.Exists(Path.GetDirectoryName(FilePath)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
            }
            using (Stream outStream = new FileStream(FilePath, FileMode.Create))
            using (StreamWriter sw = new StreamWriter(outStream, Encoding.Default))
            {
                sw.Write(FileContent);
            }
        }

        /// <summary>
        /// 从文件读取
        /// </summary>
        /// <param name="FilePath">文件绝对路径</param>
        /// <returns>文件内容</returns>
        public static String ReadFromFile(String FilePath)
        {
            String fileContent = "";
            using (Stream intStream = new FileStream(FilePath, FileMode.Open))
            using (StreamReader reader = new StreamReader(intStream, Encoding.Default))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    fileContent += (line + Environment.NewLine);
                }
            }

            return fileContent;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        public static void DeleteFile(String FilePath)
        {
            try
            {
                File.Delete(FilePath);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <returns>文件是否存在</returns>
        public static Boolean IsFileExists(String FilePath)
        {
            return File.Exists(FilePath);
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetProcessWorkingSetSize(IntPtr process, UIntPtr minimumWorkingSetSize, UIntPtr maximumWorkingSetSize);

        /// <summary>
        /// 清理内存方法
        /// </summary>
        /// <param name="removePages"></param>
        public static void ReleaseMemory(bool removePages)
        {
            GC.Collect(GC.MaxGeneration);
            GC.WaitForPendingFinalizers();
            if (removePages)
            {
                SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle,
                                         (UIntPtr)0xFFFFFFFF,
                                         (UIntPtr)0xFFFFFFFF);
            }
        }

    }
    public class TypeToString {
        //将List序列化成String
        public static string ListToString(List<string> users) {
            return JsonConvert.SerializeObject(users);
        }
        //将string反序列化成List
        public static List<string> StringToList(string UserString) {
            return JsonConvert.DeserializeObject<List<string>>(UserString);
        }
        //将Post序列化成String
        public static string PostToString(Post post) {
            return JsonConvert.SerializeObject(post);
        }
        //将string反序列化成Post
        public static Post StringToPost(string PostString) {
            return JsonConvert.DeserializeObject<Post>(PostString);
        }
    }
}
